package repository.relsys.services;

import java.util.Collection;
import java.util.Iterator;

import repository.relsys.domainmodel.general.*;

import repository.relsys.services.datacentral.FileDataProvider;
import repository.relsys.services.spring.RelsysResourceFactory;

public class RelsysFileExportImport extends DefaultRelsysService {

	public RelsysFileExportImport(String fileName, String syntax) {

		dataProvider = new FileDataProvider(fileName, syntax);
		initialize();

	}

	@SuppressWarnings("unchecked")
	public <T extends Resource> T loadResourceFromFile(Class<T> clazz) throws Exception {

		RelsysResourceFactory rf = new RelsysResourceFactory();

		Collection<T> c = (Collection<T>) loadAllResources(rf
				.getBeanImplementationClass(clazz), true);

		Iterator<T> iter = c.iterator();

		return iter.next();

	}

	public <T extends Resource> void saveResourceToFile(T resource) throws Exception {

		saveResource(resource, true);
	}

	/*
	 * public static void export(LearningTool lt, String fileName, String
	 * syntax) { try { Model m = ModelFactory.createDefaultModel();
	 * m.setNsPrefix("lt",
	 * "http://diplomski.nelakolundzija.org/LTontology.rdf#");
	 * m.setNsPrefix("swc","http://data.semanticweb.org/ns/swc/ontology#");
	 * m.setNsPrefix("jenabean", "http://thewebsemantic.com/");
	 * m.setNsPrefix("dc", "http://purl.org/dc/elements/1.1/");
	 * m.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
	 * m.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
	 * 
	 * Bean2RDF writer = new Bean2RDF(m);
	 * 
	 * writer.saveDeep(lt); m.write(new FileOutputStream(fileName), syntax);
	 * m.write(System.out, "RDF/XML"); } catch (Exception e) {
	 * System.err.println("Error!"); e.printStackTrace(); } }
	 * 
	 * public static LearningToolCls importLT(String fileName, String syntax) {
	 * 
	 * LearningToolCls ltool = null;
	 * 
	 * try {
	 * 
	 * InputStream is = new BufferedInputStream(new FileInputStream( fileName));
	 * Model m = ModelFactory.createDefaultModel().read( new
	 * InputStreamReader(is),
	 * "http://diplomski.nelakolundzija.org/LTontology.rdf#", syntax);
	 * is.close();
	 * 
	 * RDF2Bean reader = new RDF2Bean(m);
	 * 
	 * Collection<LearningToolCls> ltools = reader
	 * .loadDeep(LearningToolCls.class); Iterator<LearningToolCls> iter =
	 * ltools.iterator(); ltool = iter.next();
	 * 
	 * } catch (Exception e) { System.err.println("Error in reading");
	 * e.printStackTrace(); }
	 * 
	 * return ltool; }
	 */

}
